﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Markup;
using System.Xml;
using CasinoProject.PokerGameService;
using System.ComponentModel;
using CasinoProject.DicingGameService;


namespace CasinoProject
{
	public partial class Window1:System.Windows.Controls.Page
    {
        /// <summary>
        /// Funkcja konwertująca obiekt CasinoUser na PokerPlayer
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        PokerPlayer TranslateCasinoPlrToPokerPlayer(Admin.CasinoUser from)
        {
            PokerPlayer to = new PokerPlayer();
            to.ActualBet = 0;
            to.ID = from.user_id;
            to.Name = from.nick;
            return to;
        }
        /// <summary>
        /// Funkcja konwertująca obiekt CasinoUser na DicingPlayer
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        DicingPlayer TranslateCasinoPlayerToDicingPlayer(Admin.CasinoUser from)
        {
            DicingPlayer player = new DicingPlayer();
            player.PlayerId = from.user_id;
            player.Name = from.nick;
            //player.MoneyField=from.
            return player;
        }

        /// <summary>
        /// Konstruktor głównego okna
        /// </summary>
        /// <param name="usr">Dane użytkownika</param>
        /// <param name="MyMoney">Pieniądze uzytkownika</param>
        public Window1(Admin.CasinoUser usr, int MyMoney)
        {
            this.InitializeComponent();
            this.MyUsr = usr;
            MyWallet = MyMoney;
            txtMyWallet.Text = MyMoney.ToString() + "$";
            MyPokerPlr = new PokerPlayer();
            MyPokerPlr = TranslateCasinoPlrToPokerPlayer(usr);
            MyPokerPlr.Money = MyMoney;

            MyDicePlayer = TranslateCasinoPlayerToDicingPlayer(usr);
            MyDicePlayer.MoneyField = MyMoney;

            txtYourName.Content = usr.nick;
            txtDiceYourName.Content = usr.nick;

            Init();
        }


        /// <summary>
        /// Funkcja inicjująca większość zmiennych używanych przez klasę głównego okna
        /// </summary>
        public void Init()
        {
            ListGameRooms.SelectionMode = SelectionMode.Single;
            #region Sit places at the table
            Vector3D p1 = new Vector3D(-70, 55, 0);
            Vector3D p2 = new Vector3D(-160, 55, 0);
            Vector3D p3 = new Vector3D(-230, 25, 45);
            Vector3D p4 = new Vector3D(-230, -45, -45);
            Vector3D p9 = new Vector3D(80, 25, 45);
            Vector3D p8 = new Vector3D(80, -45, -45);
            Vector3D p10 = new Vector3D(10, 55, 0);
            Vector3D p5 = new Vector3D(-160, -55, 0);
            Vector3D p6 = new Vector3D(-70, -55, 0);
            Vector3D p7 = new Vector3D(10, -55, 0);


            PlayersPositions.Add(p1);
            PlayersPositions.Add(p2);
            PlayersPositions.Add(p3);
            PlayersPositions.Add(p4);
            PlayersPositions.Add(p5);
            PlayersPositions.Add(p6);
            PlayersPositions.Add(p7);
            PlayersPositions.Add(p8);
            PlayersPositions.Add(p9);
            PlayersPositions.Add(p10);
            #endregion

            ListButtons.Add(butDice);
            ListButtons.Add(butPoker);

            PokerPlayerMenu.Visibility = Visibility.Hidden;
            PokerPlayerMenu.Opacity = 0;

            DicePlayerMenu.Visibility = Visibility.Hidden;
            DicePlayerMenu.Opacity = 0;
            DicePlayerThrowControlCanvas.Visibility = Visibility.Hidden;
            DicePlayerThrowControlCanvas.Opacity = 0;
            DicePlayerListCanvas.Visibility = Visibility.Hidden;
            DicePlayerListCanvas.Opacity = 0;

            DiceViewControl.Visibility = Visibility.Hidden;
            DiceViewControl.Opacity = 0;

            BGWGetTableList.DoWork += new System.ComponentModel.DoWorkEventHandler(BGWGetTableList_DoWork);
            BGWGetTableList.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(BGWGetTableList_RunWorkerCompleted);
            BGWLeaveRoom.DoWork += new DoWorkEventHandler(BGWLeaveRoom_DoWork);
            BGWLeaveRoom.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWLeaveRoom_RunWorkerCompleted);
            BGConnect.DoWork += new System.ComponentModel.DoWorkEventHandler(BGConnect_DoWork);
            BGConnect.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(BGConnect_RunWorkerCompleted);
            BGCreateRoom.DoWork += new System.ComponentModel.DoWorkEventHandler(BGCreateRoom_DoWork);
            BGCreateRoom.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(BGCreateRoom_RunWorkerCompleted);
            pokerBackgroundWorker.DoWork += new DoWorkEventHandler(pokerBackgroundWorker_DoWork);
            pokerBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(pokerBackgroundWorker_RunWorkerCompleted);
            pokerBackgroundWorker.WorkerSupportsCancellation = true;
            BGWBet.DoWork += new DoWorkEventHandler(BGWBet_DoWork);
            BGWBet.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWBet_RunWorkerCompleted);
            BGWBegin.DoWork += new DoWorkEventHandler(BGWBegin_DoWork);
            BGWBegin.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWBegin_RunWorkerCompleted);
            BGWPass.DoWork += new DoWorkEventHandler(BGWPass_DoWork);
            BGWPass.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWPass_RunWorkerCompleted);


            BGWDiceGetTableList.DoWork += new DoWorkEventHandler(BGWDiceGetTableList_DoWork);
            BGWDiceGetTableList.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWDiceGetTableList_RunWorkerCompleted);
            BGWDiceLeaveRoom.DoWork += new DoWorkEventHandler(BGWDiceLeaveRoom_DoWork);
            BGWDiceLeaveRoom.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWDiceLeaveRoom_RunWorkerCompleted);
            BGDiceConnect.DoWork += new DoWorkEventHandler(BGDiceConnect_DoWork);
            BGDiceConnect.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGDiceConnect_RunWorkerCompleted);
            BGDiceCreateRoom.DoWork += new DoWorkEventHandler(BGDiceCreateRoom_DoWork);
            BGDiceCreateRoom.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGDiceCreateRoom_RunWorkerCompleted);
            diceBackgroundWorker.DoWork += new DoWorkEventHandler(diceBackgroundWorker_DoWork);
            diceBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(diceBackgroundWorker_RunWorkerCompleted);
            diceBackgroundWorker.WorkerSupportsCancellation = true;
            BGWDiceBet.DoWork += new DoWorkEventHandler(BGWDiceBet_DoWork);
            BGWDiceBet.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWDiceBet_RunWorkerCompleted);
            BGWDiceBegin.DoWork += new DoWorkEventHandler(BGWDiceBegin_DoWork);
            BGWDiceBegin.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWDiceBegin_RunWorkerCompleted);

            BGWDiceThrow.DoWork += new DoWorkEventHandler(BGWDiceThrow_DoWork);
            BGWDiceThrow.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWDiceThrow_RunWorkerCompleted);

            DiceViewControl.SimulationStopped += new SimpleEvent(diceThrow_Finished);
        }

        private void CardHand2_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (MyPokerPlr.Card1 != null)
            {
                ShowMyHand();
            }
        }


    }
}